home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-10-18 | 6.1 KB | 298 lines | [TEXT/MPS ] |
- UNIT CTdebugWriteln;
-
-
-
- INTERFACE
-
-
- PROCEDURE InstallWriteLnHook;
-
- PROCEDURE StartDebug;
-
- PROCEDURE EndDebug;
-
- PROCEDURE SetDebugLevel(newLevel: INTEGER);
-
- FUNCTION GetDebugLevel: INTEGER;
-
-
-
- IMPLEMENTATION
-
- USES
-
- Types, Traps, Memory, Quickdraw,
- OSIntf, ToolIntf, PackIntf, PPCToolbox,
-
- Events, AppleEvents,
-
- IntEnv,
-
- PasLibIntf,
- DisAsmLookup,
- Unmangler,
-
- AEregistry,
-
- CTdebugStream;
-
- VAR
-
- {$SETC qDebugProcEnds:= TRUE}
-
- gDebugProcNest: INTEGER;
- gDebugWriteln: INTEGER;
-
- {--------------------------------------------------------------------------------------------------}
-
- {$Push} {$J+}
- PFILENAME: Str255; { Name of file to intercept for IO }
-
- pDebugWindow: WindowPtr; { the window object that contains the debug
- window }
- {$Pop}
-
- {--------------------------------------------------------------------------------------------------}
-
- {$S Main}
-
- FUNCTION DevFAccess(fName: UNIV Ptr;
- opCode: Longint;
- arg: UNIV Longint): Longint;
- C; EXTERNAL;
-
- FUNCTION DevClose(fdesc: UNIV Longint): Longint;
- C; EXTERNAL;
-
- FUNCTION DevRead(fdesc: UNIV Longint;
- bufp: UNIV Longint;
- count: Longint): Longint;
- C; EXTERNAL;
-
- FUNCTION DevWrite(fdesc: UNIV Longint;
- bufp: UNIV Longint;
- count: Longint): Longint;
- C; EXTERNAL;
-
- FUNCTION DevIoctl(fdesc: UNIV Longint;
- request: Longint;
- arg: UNIV Longint): Longint;
- C; EXTERNAL;
-
- FUNCTION _addDevHandler(slot, dvName, dvFAccess, dvClose, dvRead, dvWrite,
- dvIoctl: Longint): Longint;
- C; EXTERNAL;
-
- {--------------------------------------------------------------------------------------------------}
-
- FUNCTION SetGetProc(theGetProc: ProcPtr): ProcPtr;
- EXTERNAL;
-
- FUNCTION SetPutProc(thePutProc: ProcPtr): ProcPtr;
- EXTERNAL;
-
- {--------------------------------------------------------------------------------------------------}
-
- {*****************************************************************************}
- {
- For debugging purposes only - Utility Routines
- }
-
- {$Push} {$Z+} {$%+}
-
-
- FUNCTION GetParmBlockPtr: LONGINT;
- INLINE $2E88; { MOVE.L A0,(A7) }
- { Formerly, %_GetA0. Return the value of register A0. Useful for getting the pointer
- to the parameter block from a VBL task or a completion routine. }
-
- FUNCTION GetA5: LONGINT;
- INLINE $2E8D; { MOVE.L A5,(A7) }
- { Formerly, %_GetA5. Return the value of register A5. Useful for getting the immediate value
- of A5 which is not always the same as CurrentA5. Generally a pointer to the program's
- global area and jump table. }
-
- FUNCTION GetCurStackFramePtr: Ptr;
- INLINE $2E8E; { MOVE.L A6,(A7) }
- { Formerly, %_GetA6. Return the value of register A6. Usually a pointer to the local stack
- frame. Most often used to find out the caller's name when invoking a debugging routine. }
-
- FUNCTION GetCurStackTop: Ptr;
- INLINE $2E8F; { MOVE.L A7,(A7) }
- { Formerly, %_GetA7. Return the value of register A7. Usually the top of stack. Useful
- for stack sniffing (not a crime). }
-
- PROCEDURE GetProcName(ppc: Longint;
- VAR procName: Str255);
- { GetProcName returns the name of the procedure or function in
- which ppc points. }
- VAR
- pc, nextPC, limit: Ptr;
- tmpName: Str255;
- BEGIN
- pc := Handle(ppc)^;
- IF (ord(pc) <> 0) & NOT Odd(ord(pc)) THEN
- BEGIN
- limit := Ptr(ord(pc) + 32767);
- WHILE (endOfModule(pc, limit, @procName, nextPC) = NIL) DO
- BEGIN
- IF ord(pc) >= ord(limit) THEN
- BEGIN
- procName := '';
- LEAVE;
- END
- ELSE
- pc := Ptr(ord(pc) + 2);
- END;
-
- END
- ELSE
- BEGIN
- procName := '';
- END;
- IF procName <> '' THEN
- BEGIN
- IF Unmangle(@tmpName, @procName, 255) > 0 THEN
- procName:= tmpName;
- END;
- END;
-
- PROCEDURE DumpFuncInfo(msg: Str255; aPLink, aPpc: Longint);
- VAR
- pName: Str255;
- BEGIN
- GetProcName(aPpc, pName);
-
- StrLineToDebugger(concat(msg, pName));
-
- PLFlush(output);
- END;
-
- PROCEDURE DebugNest(onEntry: BOOLEAN);
- BEGIN
- IF onEntry THEN
- gDebugProcNest:= gDebugProcNest + 1;
-
- {$IFC NOT qDebugProcEnds}
- IF onEntry THEN
- {$ENDC}
- Str255ToDebugger(copy('..............................', 1, gDebugProcNest));
-
- IF NOT onEntry THEN
- gDebugProcNest:= gDebugProcNest - 1;
- END;
-
- PROCEDURE %_BP;
- VAR
- OldA5: Longint;
- BEGIN
- { OldA5 := SetCurrentA5; } {}
-
- DebugNest(TRUE);
- DumpFuncInfo('-> ',
- Ord4(GetCurStackFramePtr), Ord4(GetCurStackFramePtr) + 4);
-
- { OldA5 := SetA5(OldA5); } {}
- END;
-
- PROCEDURE %_EP;
- VAR
- OldA5: Longint;
- BEGIN
- { OldA5 := SetCurrentA5; } {}
- DebugNest(FALSE);
-
- {$IFC qDebugProcEnds}
- DumpFuncInfo('<- ',
- Ord4(GetCurStackFramePtr), Ord4(GetCurStackFramePtr) + 4);
- {$ENDC}
-
- { OldA5 := SetA5(OldA5); } {}
- END;
-
- PROCEDURE %_EX;
- VAR
- OldA5: Longint;
- BEGIN
- { OldA5 := SetCurrentA5; } {}
- DebugNest(FALSE);
-
- {$IFC qDebugProcEnds}
- DumpFuncInfo('<x ',
- Ord4(GetCurStackFramePtr), Ord4(GetCurStackFramePtr) + 4);
- {$ENDC}
-
- { OldA5 := SetA5(OldA5); } {}
- END;
-
- {$Pop}
-
- {*****************************************************************************}
-
- PROCEDURE DebugWriteLn(textBuf: Ptr;
- byteCount: INTEGER);
- BEGIN
- IF gDebugWriteln > 0 THEN
- StreamToDebugger(textBuf, byteCount);
- END;
-
-
- FUNCTION DebugReadLn(buffer: Ptr;
- byteCount: INTEGER): Longint;
- BEGIN
- DebugReadLn:= 0;
- END;
-
-
- PROCEDURE InstallWriteLnHook;
- CONST
- kConsoleName = 'Dev:Console';
- _CODEV = 1; { console device number }
- VAR
- slot: Longint;
- oldProc: ProcPtr;
- BEGIN
- gDebugProcNest:= 0;
- gDebugWriteln:= -2;
-
- PFILENAME := kConsoleName;
- slot := _addDevHandler(_CODEV, 0, ord(@DevFAccess), ord(@DevClose), ord(@DevRead),
- ord(@DevWrite), ord(@DevIoctl));
- PLsetvbuf(output, NIL, _IOLBF, 512);
- oldProc := SetGetProc(@DebugReadLn);
- oldProc := SetPutProc(@DebugWriteLn);
- END;
-
-
-
- PROCEDURE StartDebug;
- BEGIN
- gDebugWriteln:= gDebugWriteln + 1;
- END;
-
-
-
- PROCEDURE EndDebug;
- BEGIN
- gDebugWriteln:= gDebugWriteln - 1;
- END;
-
-
- PROCEDURE SetDebugLevel(newLevel: INTEGER);
- BEGIN
- gDebugWriteln:= newLevel;
- END;
-
-
- FUNCTION GetDebugLevel: INTEGER;
- BEGIN
- GetDebugLevel:= gDebugWriteln;
- END;
-
-
- {--------------------------------------------------------------------------------------------------}
-
-
-
- END.
-